package com.turing.post.datatranfer.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.common.util.ContrastDao;
import com.turing.post.datatranfer.util.OldIssueBranchBusiness;
import com.turing.post.datatranfer.util.OldIssueBranchDao;
import com.turing.post.datatranfer.util.TranferApplicationException;
import com.turing.post.organization.util.IssueBranch;
import com.turing.post.organization.util.IssueBranchBusiness;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 业务实现接口
 * 
 * @author WZD
 */
@Stateless(mappedName = "datatranfer.impl.OldIssueBranchBusinessImpl")
@Remote
@Local
public class OldIssueBranchBusinessImpl implements OldIssueBranchBusiness {

	@EJB(mappedName = "datatranfer.impl.OldIssueBranchDaoImpl")
	private OldIssueBranchDao oldIssueBranchDaoImpl;
	@EJB(mappedName = "organization.impl.IssueBranchBusinessImpl")
	private IssueBranchBusiness issueBranchBusinessImpl;// 接口

	@EJB(mappedName = "common.impl.ContrastDaoImpl")
	private ContrastDao contrastDaoImpl;// 对照表接口
	public void insertNewIssueBranch(PCPSWebInput webInput,String issuecompanyno,String regionNo) throws PCPSApplicationException{
		// 检查发卡商机构是否存在
		if (PCPSUtil.isEmpty(issuecompanyno)) {
			throw new CapitalApplicationException(
					TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "发卡商为空!");
		}
		// 检查发卡商机构是否存在
		if (PCPSUtil.isEmpty(regionNo)) {
			throw new CapitalApplicationException(
					TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "行政区域为空!");
		}
		List<Object[]> list = oldIssueBranchDaoImpl.getIssueBranchList();
		IssueBranch issueBranch1 = new IssueBranch();
		issueBranch1.setIssueBranchNo(issuecompanyno+"99"+regionNo.substring(0,2));
		issueBranch1.setIssueBranchName("省级发卡机构");
		issueBranch1.setSuperIssueBranchNo(issuecompanyno+"99");
		issueBranch1.setRegionNo(regionNo.substring(0,2));
		issueBranch1.setStatus("1");
		issueBranch1.setAllowActivate("0");
		issueBranch1.setActivateBalance(new BigInteger("0"));
		issueBranch1.setCreditBalance(new BigInteger("0"));
		issueBranchBusinessImpl.insertIssueBranchInfo(webInput, issueBranch1);
		
		IssueBranch issueBranch2 = new IssueBranch();
		issueBranch2.setIssueBranchNo(issuecompanyno+"99"+regionNo.substring(0,4));
		issueBranch2.setIssueBranchName("市级发卡机构");
		issueBranch2.setSuperIssueBranchNo(issuecompanyno+"99"+regionNo.substring(0,2));
		issueBranch2.setRegionNo(regionNo.substring(0,4));
		issueBranch2.setStatus("1");
		issueBranch2.setAllowActivate("0");
		issueBranch2.setActivateBalance(new BigInteger("0"));
		issueBranch2.setCreditBalance(new BigInteger("0"));
		issueBranchBusinessImpl.insertIssueBranchInfo(webInput, issueBranch2);
		
		IssueBranch issueBranch3 = new IssueBranch();
		issueBranch3.setIssueBranchNo(issuecompanyno+"99"+regionNo);
		issueBranch3.setIssueBranchName("区级发卡机构");
		issueBranch3.setSuperIssueBranchNo(issuecompanyno+"99"+regionNo.substring(0,4));
		issueBranch3.setRegionNo(regionNo.substring(0,4));
		issueBranch3.setStatus("1");
		issueBranch3.setAllowActivate("0");
		issueBranch3.setActivateBalance(new BigInteger("0"));
		issueBranch3.setCreditBalance(new BigInteger("0"));
		issueBranchBusinessImpl.insertIssueBranchInfo(webInput, issueBranch3);
		
		Object[] obj = null;
		IssueBranch issueBranch = null;
		PCPSWebInput webInput1=new PCPSWebInput();
		webInput1.setOperBranchNo(issuecompanyno);
		webInput1.setOperId(new BigInteger("9"));
		for (int i = 0; i < list.size(); i++) {
			obj = list.get(i);
			if(contrastDaoImpl.getMapIssueBranchByNo(obj[0].toString())==null){
				issueBranch = new IssueBranch();
				String no=String.format("%02d", i);
				no=issuecompanyno+"99"+regionNo+no;
				issueBranch.setIssueBranchNo(no);
				issueBranch.setIssueBranchName(obj[1].toString());
				issueBranch.setSuperIssueBranchNo(issuecompanyno+"99"+regionNo);
				issueBranch.setRegionNo(regionNo);
				issueBranch.setStatus("1");
				issueBranch.setAllowActivate("0");
				issueBranch.setActivateBalance(new BigInteger("0"));
				issueBranch.setCreditBalance(new BigInteger("0"));
				issueBranchBusinessImpl.insertIssueBranchInfo(webInput1, issueBranch);
				
				contrastDaoImpl.insertMapIssueBranch(obj[0].toString(), no);
			}
		}
	}
}
